package com.zenith.edu.controller;

import com.zenith.common.annotation.request.SecurePostMapping;
import com.zenith.common.result.Result;
import com.zenith.edu.po.dto.EduSchoolDTO;
import com.zenith.edu.po.vo.EduSchoolVO;
import com.zenith.edu.service.EduSchoolService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/edu/school")
@Tag(name = "学校管理", description = "学校管理接口")
public class EduSchoolController {
    
    @Autowired
    private EduSchoolService eduSchoolService;
    
    @SecurePostMapping(value = "/createSchool", roles = "ROLE_ADMIN", permission = "school:create")
    @Operation(summary = "创建学校")
    public Result<EduSchoolVO> createSchool(@RequestBody EduSchoolDTO dto) {
        EduSchoolVO schoolVO = eduSchoolService.createSchool(dto);
        return Result.success(schoolVO);
    }
    
    @SecurePostMapping(value = "/getSchoolById/{id}", roles = "ROLE_ADMIN", permission = "school:read")
    @Operation(summary = "根据ID获取学校详情")
    public Result<EduSchoolVO> getSchoolById(@PathVariable Long id) {
        EduSchoolVO schoolVO = eduSchoolService.getSchoolById(id);
        if (schoolVO == null) {
            return Result.fail("学校不存在");
        }
        return Result.success(schoolVO);
    }
    
    @SecurePostMapping(value = "/updateSchool", roles = "ROLE_ADMIN", permission = "school:update")
    @Operation(summary = "更新学校信息")
    public Result<EduSchoolVO> updateSchool(@RequestBody EduSchoolDTO dto) {
        EduSchoolVO schoolVO = eduSchoolService.updateSchool(dto);
        return Result.success(schoolVO);
    }
    
    @SecurePostMapping(value = "/deleteSchool/{id}", roles = "ROLE_ADMIN", permission = "school:delete")
    @Operation(summary = "删除指定学校")
    public Result<Void> deleteSchool(@PathVariable Long id) {
        boolean result = eduSchoolService.deleteSchool(id);
        if (!result) {
            return Result.fail("学校删除失败");
        }
        return Result.success();
    }
    
    @SecurePostMapping(value = "/getSchoolList", roles = "ROLE_ADMIN", permission = "school:read")
    @Operation(summary = "获取学校列表")
    public Result<List<EduSchoolVO>> getSchoolList() {
        List<EduSchoolVO> schoolList = eduSchoolService.getSchoolList();
        return Result.success(schoolList);
    }
}